package com.weitu.permission.departmentDuty.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.permission.departmentDuty.service.DepartmentDutyService;
import com.weitu.permission.duty.entity.Duty;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("permission/departmentDuty")
class DepartmentDutyController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/departmentDuty";


    private final DepartmentDutyService departmentDutyService;

    @Autowired
    public DepartmentDutyController(DepartmentDutyService departmentDutyService) {
        this.departmentDutyService = departmentDutyService;
    }


    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String listView() {
        return MODULE_BATH_PATH + "/list";
    }

    @ResponseBody
    @RequestMapping("/findByParam")
    public List<Duty> findByParam() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        putParametersIntoMap(paramMap);
        return departmentDutyService.findByParam(paramMap);

    }

    @ResponseBody
    @RequestMapping(value = "/add")
    @SystemLog(module = "部门职位管理",methods = "维护部门职位关系")
    public AjaxResult add() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        putParametersIntoMap(paramMap);

        String dutyIds = paramMap.get("dutyIds").toString();
        String deptId = paramMap.get("deptId").toString();

        String[] dutyIdArray = dutyIds.split(",");

        List<Map> list = new ArrayList<Map>();

        Map<String, String> map;

        for (String dutyId : dutyIdArray) {
            map = new HashMap<String, String>();
            map.put("deptId", deptId);
            map.put("dutyId", dutyId);
            list.add(map);
        }

        Integer result = departmentDutyService.add(list, deptId);

        return resultAjaxResult(result);
    }

    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }


}
